<script setup lang="ts">
import {onMounted, ref, watchEffect} from 'vue'
import {useRouter} from 'vue-router'
import moment from 'moment'
import {
  ProblemSubmitControllerService,
  ProblemSubmitQueryRequest,
  ProblemSubmitVO,
} from '/@/api/openapi'
import {ElMessage} from 'element-plus'

const tableRef = ref()
const formatNumber = (src: number, pos: number) => {
  const data = Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos)
  return data.toFixed(2)
}
const dataList = ref<ProblemSubmitVo[]>([])
const total = ref(0)

const handleCurrentChange = (val: number) => {
  console.log('修改了page', val)
  searchParams.value.pageNum = val
  loadData()
}
const searchParams = ref<ProblemSubmitQueryRequest>({
  problemId: undefined,
  uid: undefined,
  pageSize: 10,
  pageNum: 1,
  sortField: 'create_time',
  sortOrder: 'descend',
})

const loadData = async () => {
  console.log("==============ProblemSubmitView查询参数===========")
  console.log(searchParams.value)
  const res = await ProblemSubmitControllerService.listProblemSubmitVoByPage(
    searchParams.value,
  )
  if (res.code === 200) {
    if (res.data === undefined || res.data.records === undefined) {
      return;
    }

    dataList.value = res.data.records
    total.value = res.data?.total as number
  } else {
    ElMessage({
      type: 'error',
      message: res.message,
    })
  }
}

/**
 * 监听 searchParams 变量，改变时触发页面的重新加载
 */
// watchEffect(() => {
//   loadData()
// })

/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData()
})

const router = useRouter()

/**
 * 跳转到做题页面
 * @param question
 */
// const toQuestionPage = (question: ) => {
//   router.push({
//     path: `/view/question/${question.id}`,
//   })
// }

/**
 * 确认搜索，重新加载数据
 */
const doSubmit = () => {
  // 这里需要重置搜索页号
  searchParams.value = {
    ...searchParams.value,
    pageNum: 1,
  }

  loadData()
}
</script>


<template>
    <div id="questionSubmitView">
        <el-card class="card">
            <el-form class="search-form" :data="searchParams" :inline="true">
                <el-form-item prop="questionId" label="题号" style="min-width: 240px">
                    <el-input type="number" v-model="searchParams.problemId" placeholder="请输入"/>
                </el-form-item>

                <el-form-item
                        field="language"
                        label="编程语言"
                        style="min-width: 240px"
                >
                    <el-radio-group v-model="searchParams.language" size="large">
                        <el-radio-button value="">全部</el-radio-button>
                        <el-radio-button value="java" >java</el-radio-button>
                        <el-radio-button value="cpp" >cpp</el-radio-button>
                        <el-radio-button value="go" >go</el-radio-button>
                        <el-radio-button value="html">html</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click.stop="doSubmit">搜索</el-button>
                </el-form-item>
            </el-form>
            <el-divider size="0"/>
            <el-table :ref="tableRef" :data="dataList">
                <el-table-column
                        prop="problemId"
                        label="题号"
                        width="100"
                        align="center"
                />
                <el-table-column
                        prop="language"
                        label="编程语言"
                        width="100"
                        align="center"
                />
                <el-table-column
                        prop="status"
                        label="题目状态"
                        width="100"
                        align="center"
                />

                <el-table-column label="判题状态" align="center">
                    <template #default="scope">
                        <el-tag type="success" effect="dark" v-if="scope.row.status === 3"
                        >通过
                        </el-tag>
                        <el-tag
                                type="danger"
                                effect="dark"
                                v-else-if="scope.row.status === 2"
                        >失败
                        </el-tag>
                        <el-tag
                                type="warning"
                                effect="dark"
                                v-else-if="scope.row.status === 1"
                        >判题中
                        </el-tag>
                        <el-tag type="info" effect="dark" v-else>待判题</el-tag>
                    </template>
                </el-table-column>

                <el-table-column label="判题信息" align="center">
                    <template #default="scope">
                        {{ scope.row.judgeInfo.status }}
                    </template>
                </el-table-column>

                <el-table-column label="内存" align="center">
                    <template #default="scope">
                        {{ scope.row.judgeInfo.time }}
                    </template>
                </el-table-column>
                <el-table-column label="内存" align="center">
                    <template #default="scope">
                        {{ scope.row.judgeInfo.memory }}
                    </template>
                </el-table-column>

                <el-table-column label="通过率" align="center">
                    <template #default="scope">
                        {{
                        `${
                            scope.row.judgeInfo.total
                                ? formatNumber(
                                    Number(scope.row.judgeInfo.pass) /
                                    Number(scope.row.judgeInfo.total),
                                    2,
                                )
                                : '0'
                        }% (${scope.row.judgeInfo.pass}/${scope.row.judgeInfo.total})`
                        }}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="userName"
                        label="用户姓名"
                        width="100"
                        align="center"
                />
                <el-table-column label="创建时间" align="center">
                    <template #default="scope">
                        {{ moment(scope.row.createTime).format('YYYY-MM-DD') }}
                    </template>
                </el-table-column>
            </el-table>

            <div class="pager-wrapper">
                <el-pagination
                        background
                        layout="prev, pager, next, jumper"
                        :total="total"
                        :page-size="searchParams.pageSize"
                        :currentPage="searchParams.pageNum"
                        @current-change="handleCurrentChange"
                />
            </div>
        </el-card>
    </div>
</template>


<style scoped>
#questionSubmitView {
    max-width: 1280px;
    margin: 0 auto;
}

.pager-wrapper {
    margin-top: 20px;
    float: right;
}

.search-form {
    margin-top: 20px;
}

.card {
    padding: 10px;
}
</style>
